<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" @submit.native.prevent
            v-show="showSearch" label-width="68px">
            <el-form-item label="编号" prop="no">
                <el-input v-model="queryParams.no" placeholder="请输入编号" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="托盘号" prop="trayNo">
                <el-input v-model="queryParams.trayNo" placeholder="请输入托盘号" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="单据状态" prop="orderStatus">
                <el-select v-model="queryParams.orderStatus" placeholder="请选择单据状态"  >
                <el-option
                    v-for="dict in dict.type.stock_bill_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"-->
<!--                    v-hasPermi="['inOutStock:outStockBill:add']">新增</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"-->
<!--                    v-hasPermi="['inOutStock:outStockBill:edit']">修改</el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"-->
<!--                    @click="handleDelete" v-hasPermi="['inOutStock:outStockBill:remove']">删除</el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['inOutStock:outStockBill:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table ref="table" :height="calcTableHeight" v-loading="loading" :data="outStockBillList"
            @selection-change="handleSelectionChange" highlight-current-row>
            <el-table-column label="序号" type="index" width="50" />
            <af-table-column label="编号" align="center" prop="no" width="180">
                <template slot-scope="scope">
                    <span class="link-pop"  @click="rowSelection(scope.row)">{{ scope.row.no }}</span>
                </template>
            </af-table-column>
            <el-table-column label="料架号" align="center" prop="shelfNo" width="120" show-overflow-tooltip />
            <el-table-column label="托盘号" align="center" prop="trayNo" width="120" show-overflow-tooltip />
            <el-table-column label="源库位号" align="center" prop="sourceLocation" width="120" show-overflow-tooltip />
            <el-table-column label="目标库位号" align="center" prop="destLocation" width="120" show-overflow-tooltip />
            <el-table-column label="单据状态" align="center" prop="orderStatus" width="80" show-overflow-tooltip >
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.stock_bill_status" :value="scope.row.orderStatus"/>
                </template>
            </el-table-column>
            <el-table-column label="关联单据" align="center" prop="relOrderNo" width="180" show-overflow-tooltip />
            <el-table-column label="完成时间" align="center" prop="completeTime" width="155" show-overflow-tooltip>
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.completeTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark" min-width="120" show-overflow-tooltip />
            <el-table-column label="排序" align="center" prop="sort" width="60" show-overflow-tooltip />
            <el-table-column label="是否分捡" align="center" prop="isSplit" width="80" show-overflow-tooltip />
            <el-table-column label="创建人" align="center" prop="createBy" width="80" show-overflow-tooltip />
            <el-table-column label="创建时间" align="center" prop="createTime" width="155" show-overflow-tooltip />
            <el-table-column label="最后更新人" align="center" prop="updateBy" width="100" show-overflow-tooltip />
            <el-table-column label="最后更新时间" align="center" prop="updateTime" width="155" show-overflow-tooltip />
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改出库单对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="编号" prop="no">
                    <el-input v-model="form.no" placeholder="请输入编号" />
                </el-form-item>
                <el-form-item label="料架号" prop="shelfNo">
                    <el-input v-model="form.shelfNo" placeholder="请输入料架号" />
                </el-form-item>
                <el-form-item label="托盘号" prop="trayNo">
                    <el-input v-model="form.trayNo" placeholder="请输入托盘号" />
                </el-form-item>
                <el-form-item label="源库位号" prop="sourceLocation">
                    <el-input v-model="form.sourceLocation" placeholder="请输入源库位号" />
                </el-form-item>
                <el-form-item label="目标库位号" prop="destLocation">
                    <el-input v-model="form.destLocation" placeholder="请输入目标库位号" />
                </el-form-item>
                <el-form-item label="关联单据" prop="relOrderNo">
                    <el-input v-model="form.relOrderNo" placeholder="请输入关联单据" />
                </el-form-item>
                <el-form-item label="完成时间" prop="completeTime">
                    <el-date-picker clearable v-model="form.completeTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="请选择完成时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input v-model="form.sort" placeholder="请输入排序" />
                </el-form-item>
                <el-form-item label="是否分捡" prop="isSplit">
                    <el-input v-model="form.isSplit" placeholder="请输入是否分捡" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!--   单据明细 -->
 

           <!-- 明细弹窗 -->
        <el-dialog :title="'明细'" :visible.sync="detailModal" width="1000px" append-to-body>
            <el-tabs v-model="activeTab" type="card" ref="halfTable" class="half-table">
                <el-tab-pane label="出库单明细" name="detailList">
                    <detailPage ref="detailPage" style="padding: unset" :orderData="orderData" />
                </el-tab-pane>
            </el-tabs>
          <div slot="footer" class="dialog-footer">
            <el-button @click="detailModal = false">关 闭</el-button>
          </div>
        </el-dialog>
    </div>
</template>

<script>
import { listOutStockBill, getOutStockBill, delOutStockBill, addOutStockBill, updateOutStockBill } from "@/api/inOutStock/outStockBill";
import detailPage from "./detail"
import { mixins } from "@/mixin/pageSearchTable";
export default {
    name: "OutStockBill",
    components: { detailPage },
    dicts: ['stock_bill_status'],
    mixins: [mixins],
    data() {
        return {
            detailModal: false,
            activeTab: "detailList",
            orderData:{},
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            selections: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 出库单表格数据
            outStockBillList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                no: null,
                shelfNo: null,
                trayNo: null,
                sourceLocation: null,
                destLocation: null,
                orderStatus: null,
                relOrderNo: null,
                completeTime: null,
                sort: null,
                isSplit: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                no: [
                    { required: true, message: "编号不能为空", trigger: "blur" }
                ],
                trayNo: [
                    { required: true, message: "托盘号不能为空", trigger: "blur" }
                ],
                orderStatus: [
                    { required: true, message: "单据状态不能为空", trigger: "blur" }
                ],
                delFlag: [
                    { required: true, message: "是否删除不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        //行选择
        rowSelection(row) {
            this.detailModal = true
            this.$nextTick(() => {
                this.activeTab = "detailList"
                this.orderData = row
            })
        },
        /** 查询出库单列表 */
        getList() {
            this.loading = true;
            listOutStockBill(this.queryParams).then(response => {
                let orderData = { ...this.orderData }
                this.outStockBillList = response.rows;
                this.total = response.total;
                this.loading = false;
                this.$nextTick(() => {
                    if (orderData && orderData.id) {
                        let row = response.rows.find(item => item.id == orderData.id)
                        this.$refs.table.setCurrentRow(row);
                    }
                })



            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                no: null,
                shelfNo: null,
                trayNo: null,
                sourceLocation: null,
                destLocation: null,
                orderStatus: 0,
                relOrderNo: null,
                completeTime: null,
                remark: null,
                delFlag: null,
                createBy: null,
                updateBy: null,
                updateTime: null,
                createTime: null,
                sort: null,
                isSplit: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.selections = selection
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加出库单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getOutStockBill(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改出库单";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateOutStockBill(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addOutStockBill(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.resetQuery();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除"' + ids.length + '"条数据项？').then(function () {
                return delOutStockBill(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('inOutStock/outStockBill/export', {
                ...this.queryParams
            }, `outStockBill_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>
